# Maintainer: Alexey Pavlov <alexpux@gmail.com>
_variant=-openssl
#_variant=-winssl
#_variant=-gnutls

_realname=curl
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=7.50.1
pkgrel=1
pkgdesc="An URL retrival utility and library. (mingw-w64)"
arch=('any')
url="https://curl.haxx.se"
license=("MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-libidn"
         "${MINGW_PACKAGE_PREFIX}-libmetalink"
         "${MINGW_PACKAGE_PREFIX}-libssh2"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-rtmpdump"
         $([[ "$_variant" == "-openssl" ]] && echo \
            "${MINGW_PACKAGE_PREFIX}-ca-certificates" \
            "${MINGW_PACKAGE_PREFIX}-openssl" \
            "${MINGW_PACKAGE_PREFIX}-nghttp2")
         $([[ "$_variant" == "-gnutls" ]] && echo \
            "${MINGW_PACKAGE_PREFIX}-ca-certificates" \
            "${MINGW_PACKAGE_PREFIX}-gnutls")
         )
options=('staticlibs')
source=("${url}/download/${_realname}-${pkgver}.tar.bz2"{,.asc}
        "0001-curl-relocation.patch"
        "0004-cURL-Get-relocatable-base-from-dll.patch")
sha256sums=('3c12c5f54ccaa1d40abc65d672107dcc75d3e1fcb38c267484334280096e5156'
            'SKIP'
            '3aa3e42dc35daba84470def39d7b6e16ce15f3397e99d341b844d8cd8796bab5'
            'e21fb106ebda8559ba2d34633a9d7d94be541de02de01982f92d67048bb9854b')
validpgpkeys=('914C533DF9B2ADA2204F586D78E11C6B279D5C91'  # Daniel Stenberg
              '27EDEAF22F3ABCEB50DB9A125CC908FDB71E12C2'
              '4461EAF0F8E9097F48AF0555F9FEAFF9D34A1BDB')

prepare() {
  cd "${_realname}-${pkgver}"
  rm -f lib/pathtools.h lib/pathtools.c > /dev/null 2>&1 || true
  patch -p1 -i "${srcdir}/0001-curl-relocation.patch"
  patch -p1 -i "${srcdir}/0004-cURL-Get-relocatable-base-from-dll.patch"
  autoreconf -vfi
}

build() {
  local -a extra_config
  if check_option "debug" "y"; then
    extra_config+=( --enable-debug )
  else
    extra_config+=( --disable-debug )
  fi
  mkdir -p "${srcdir}/build-${CARCH}"
  local -a _variant_config
  if [ "${_variant}" = "-winssl" ]; then
    _variant_config+=("--without-ssl")
    _variant_config+=("--without-gnutls")
    _variant_config+=("--with-winssl")
    _variant_config+=('--without-nghttp2')
    _variant_config+=("--without-ca-bundle")
    _variant_config+=("--without-ca-path")  
  elif [ "${_variant}" = "-gnutls" ]; then
    _variant_config+=("--without-ssl")
    _variant_config+=("--with-gnutls")
    _variant_config+=('--without-nghttp2')
    _variant_config+=("--with-ca-bundle=${MINGW_PREFIX}/ssl/certs/ca-bundle.crt")
  elif [ "${_variant}" = "-openssl" ]; then
    _variant_config+=("--without-gnutls")
    _variant_config+=("--with-ssl")
    _variant_config+=("--with-ca-bundle=${MINGW_PREFIX}/ssl/certs/ca-bundle.crt")
    _variant_config+=('--with-nghttp2=${MINGW_PREFIX}/')
  fi
  cd "${srcdir}/build-${CARCH}"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --without-random \
    --enable-static \
    --enable-shared \
    --enable-sspi \
    --with-libmetalink=${MINGW_PREFIX} \
    "${_variant_config[@]}" \
    "${extra_config[@]}"
# there's a bug with zsh completion generation script and Windows.
# curl has to specified with the file extension.
  sed -i "s|\/curl > \$\@|\/curl\$\{EXEEXT\} > \$\@|" scripts/Makefile
  make
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" install
}
